<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * date: 2021-12-01 09:56:57
 * @LastEditors: Sam
 * @LastEditTime: 2021-12-08 20:54:12
 */


namespace app\admin\model;


/**
 * Class StoreCategory
 * @package app\admin\model\store
 */
class StoreCoupon extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'store_coupon'; //优惠券模板



    /**
     * @param $where
     * @return array
     */
    public static function systemPage($where)
    {
        $model = new self;
        if ($where['status'] != '') $model = $model->where('status', $where['status']);
        if ($where['type'] != '') $model = $model->where('type', $where['type']);
        if ($where['title'] != '') $model = $model->where('title', 'LIKE', "%$where[title]%");
//        if($where['is_del'] != '')  $model = $model->where('is_del',$where['is_del']);
        $model = $model->where('is_del', 0);
        $model = $model->order('id desc');
        return self::page($model, $where);
    }

    /**
     * @param $where
     * @return array
     */
    public static function systemPageCoupon($where)
    {
        $model = new self;
        if ($where['status'] != '') $model = $model->where('status', $where['status']);
        if ($where['title'] != '') $model = $model->where('title', 'LIKE', "%$where[title]%");
//        if($where['is_del'] != '')  $model = $model->where('is_del',$where['is_del']);
        $model = $model->where('is_del', 0);
        $model = $model->where('status', 1);
        $model = $model->order('sort desc,id desc');
        return self::page($model, $where);
    }

    public static function editIsDel($id)
    {
        $data['status'] = 0;
        self::beginTrans();
        $res1 = self::edit($data, $id);
        $res2 = false !== StoreCouponUser::where('cid', $id)->update(['is_fail' => 1]);
        $res3 = false !== StoreCouponIssue::where('cid', $id)->update(['status' => -1]);
        $res = $res1 && $res2 && $res3;
        self::checkTrans($res);
        return $res;

    }

    public function getTypeNameAttr($value,$data){
        $type = [0=>'通用券',1=>'品类券',2=>'商品券'];
        return $type[$data['type']];
    }
}